package com.zzvcom.webapp.action.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzvcom.persist.model.admin.Article;
import com.zzvcom.service.admin.ArticleManager;
import com.zzvcom.template.ResultPage;
import com.zzvcom.utils.DateUtil;
import com.zzvcom.utils.ExtMessage;

@Controller
@Scope("prototype")
public class ArticleAction
{

    private Article article;

    private ArticleManager manager;

    private ExtMessage msg = new ExtMessage();// 返回信息

    private Map rs = new HashMap();// 结果集合

    private ResultPage resultPage;
    
    private Integer start;

    private Integer limit;

    public String execute()
    {
        if (resultPage == null)
        {
            resultPage = new ResultPage();
        }
        //System.out.println("start limit"+start+":"+limit);
        resultPage.startLimitSetting(start, limit);
        DetachedCriteria dc = DetachedCriteria.forClass(Article.class);
        dc.addOrder(Order.desc("seq"));
        dc.addOrder(Order.desc("addtime"));
        resultPage.setDetachedCriteria(dc);        
        manager.getResultPageByDetachedCriteria(resultPage);
        rs.put("root", resultPage.getResult());
        rs.put("records", resultPage.getTotalRecord());
        return "rs";
    }
    
    public String find()
    {        
        article = manager.get(article.getId());       
        return "article";   
    }

    public String save()
    {
        if (StringUtils.isBlank(article.getId()))
        {
            article.setId(null);
        }
        article.setAddtime(DateUtil.dateToNum14(new Date()));
        manager.save(article);
        msg.setInfo("保存成功");
        return "msg";
    }

    public String delete()
    {
        article = manager.get(article.getId());
        manager.delete(article);
        msg.setInfo("删除成功");
        return "msg";
    }

    public ResultPage getResultPage()
    {
        return resultPage;
    }

    public void setResultPage(ResultPage resultPage)
    {
        this.resultPage = resultPage;
    }

    public Article getArticle()
    {
        return article;
    }

    public void setArticle(Article article)
    {
        this.article = article;
    }

    public ArticleManager getManager()
    {
        return manager;
    }

    @Autowired
    public void setManager(ArticleManager manager)
    {
        this.manager = manager;
    }

    public ExtMessage getMsg()
    {
        return msg;
    }

    public void setMsg(ExtMessage msg)
    {
        this.msg = msg;
    }

    public Map getRs()
    {
        return rs;
    }

    public void setRs(Map rs)
    {
        this.rs = rs;
    }

    public Integer getStart()
    {
        return start;
    }

    public void setStart(Integer start)
    {
        this.start = start;
    }

    public Integer getLimit()
    {
        return limit;
    }

    public void setLimit(Integer limit)
    {
        this.limit = limit;
    }

}
